package com.bbex.service.impl;

import com.bbex.constant.Constants;
import com.bbex.entity.PlatUser;
import com.bbex.entity.UserTagApply;
import com.bbex.entity.UserTagConfig;
import com.bbex.exception.PlatException;
import com.bbex.mapper.PlatUserDao;
import com.bbex.mapper.UserTagApplyDao;
import com.bbex.mapper.UserTagConfigDao;
import com.bbex.service.UserCoinVolumeExService;
import com.bbex.service.UserTagApplyService;
import com.bbex.util.SnowFlake;
import com.bbex.vo.UserTagApplyVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service("userTagApplyService")
public class UserTagApplyServiceImpl implements UserTagApplyService {

	private static Logger logger = LoggerFactory.getLogger(UserTagApplyServiceImpl.class);
	
	@Autowired
	private UserTagConfigDao userTagConfigDao;
	
	@Autowired
	private UserTagApplyDao userTagApplyDao;
	
	@Autowired
    private PlatUserDao platUserDao;
	
	@Autowired
	private UserCoinVolumeExService userCoinVolumeService;
	
	/**
	 * 用户申请tag
	 */
	@Transactional
	@Override
	public void applyTag(UserTagApplyVO userTagApplyVO) {
		String userId = userTagApplyVO.getUserId() ;
		//第一用户的要求码为顶级邀请码
		PlatUser platUser = platUserDao.findById(userId);
		String referInviteCode = platUser.getReferInviteCode() ;
		if(StringUtils.isNotBlank(referInviteCode)) {
			logger.warn("用户id:{},申请用户超级节点等级不够。",userId);
			throw new PlatException(Constants.GLOBAL_ERROR_CODE,"不能申请超级节点,请联系客服");
		}
		if(StringUtils.isNotBlank(platUser.getTag())) {
			logger.warn("用户id:{},不要重复申请。请重新登录。",userId);
			throw new PlatException(Constants.GLOBAL_ERROR_CODE,"你已经申请成功了,不要重复申请。请重新登录");
		}
		//查询申请的等级配置
		UserTagConfig userTagConfig = userTagConfigDao.findByTagCode(userTagApplyVO.getTagCode());
		if(userTagConfig == null) {
			logger.warn("用户id:{},申请用户节点tagCode:{},没有配置该节点",userId,userTagApplyVO.getTagCode());
			throw new PlatException(Constants.GLOBAL_ERROR_CODE,"不能申请超级节点,请联系客服");
		}
		//添加申请记录
		String id = SnowFlake.createSnowFlake().nextIdString();
		UserTagApply userTagApply = new UserTagApply();
		userTagApply.setCoinSymbol(userTagConfig.getCoinSymbol());
		//userTagApply.setCreateBy("0");
		userTagApply.setCreateDate(LocalDateTime.now());
		userTagApply.setId(id);
		userTagApply.setMail(platUser.getMail());
		userTagApply.setMobile(platUser.getMobile());
		userTagApply.setTagCode(userTagApplyVO.getTagCode());
		//userTagApply.setUpdateBy("0");
		userTagApply.setUpdateDate(LocalDateTime.now());
		userTagApply.setUserId(userId);
		userTagApply.setVolume(userTagConfig.getVolume());
		userTagApplyDao.insert(userTagApply);
		//修改用户tag
		PlatUser updateTag = new PlatUser();
		updateTag.setTag(userTagApplyVO.getTagCode());
		//updateTag.setRemarks("修改用户tag");
		updateTag.setId(platUser.getId());
		platUserDao.updateById(updateTag);
		//查询该用户的资产 扣除用户资产
		logger.info("审核tag改变  bb update userId: " + userId + " bbVolume:" + userTagConfig.getVolume());
	    userCoinVolumeService.updateOutcomeException(null,userTagConfig.getVolume(),userId,userTagConfig.getCoinSymbol());
	}

	@Override
	public UserTagConfig applyTagConfig(UserTagApplyVO userTagApplyVO) {
		return userTagConfigDao.findByTagCode(userTagApplyVO.getTagCode());
	}
	
	

}
